@extends('Admin.base')
@section('css')
    <link rel="stylesheet" href="/Admin/layuiadmin/css/extend/formSelects-v4.css" media="all">
@endsection
@section('style')

@endsection
@section('content')

    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-bg-gray">
            @if($index == 1)
                销售中商品
            @elseif($index == 2)
                已下架商品
            @elseif($index == 3)
                已售罄商品
            @elseif($index == 4)
                商品价格查询
            @else
                商品列表
            @endif
            <div class="layui-form-item layui-row">
                <h3>快速查询</h3>
                <div class="layui-col-md6" >
                    <label class="layui-form-label">关键词</label>
                    <div class="layui-input-block">
                        <input type="text" name="keyword" placeholder="请输入商品名称、ID、品牌" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">会员性质</label>
                    <div class="layui-input-block">
                        <select name="is_sale">
                            <option value="0">不限</option>
                            @foreach (\App\Models\Goods::IS_SALE as $id => $name)
                                <option value="{{$id ?? 0}}">{{$name ?? ''}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">优惠券属性</label>
                    <div class="layui-input-block">
                        <select name="is_coupon">
                            <option value="0">不限</option>
                            @foreach (\App\Models\Goods::IS_COUPON as $id => $name)
                                <option value="{{$id ?? 0}}">{{$name ?? ''}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">功效</label>
                    <div class="layui-input-block">
                        <select name="effect">
                            <option value="0">不限</option>
                            @foreach ($effect as $v)
                                <option value="{{$v['id'] ?? 0}}">{{$v['name'] ?? ''}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">所属模块</label>
                    <div class="layui-input-block">
                        <select name="module">
                            <option value="0">不限</option>
                            @foreach ($home as $v)
                                <option value="{{$v['id']?? 0}}">{{$v['module'] ?? ''}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-btn-group layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-input-block layuiadmin-btn-admin" lay-submit lay-filter="LAY-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-sm layui-input-block layuiadmin-btn-admin" id="LAY-clear">
                            清空
                        </button>

                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-table" lay-filter="LAY-table"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    @if($index == 1 or $index == 2 )
                        <a class="layui-btn" href="{{route('goods.add')}}">添加商品</a>
                    @endif
                    @if($index == \App\Models\Goods::SALE_STATUS_OPEN or $index == \App\Models\Goods::SALE_STATUS_OUT)
                        <button class="layui-btn" lay-event="status">下 架</button>
                    @elseif($index == \App\Models\Goods::SALE_STATUS_CLOSE)
                        <button class="layui-btn" lay-event="status">上 架</button>
                    @endif
                    <button class="layui-btn" lay-event="export">导 出</button>
                </div>
            </script>
            <script type="text/html" id="table-toolbar">
                @if($index !=4)
                <a href="{{url('admin/goods/')}}/@{{ d.id }}/edit" class="layui-btn layui-btn-green layui-btn-xs">
                    编辑
                </a>
                @endif
                @if($index == \App\Models\Goods::SALE_STATUS_CLOSE)
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
                    删除
                </a>
                @endif
                {{--@if($index == \App\Models\Goods::SALE_STATUS_OUT)--}}
                {{--<a  class="layui-btn layui-btn-danger layui-btn-xs" lay-event="status">--}}
                    {{--下架--}}
                {{--</a>--}}
                {{--@endif--}}
            </script>
        </div>
    </div>
@endsection
@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['form', 'table', 'formSelects'], function(){
            var $ = layui.$
                ,form = layui.form
                ,table = layui.table,
                formSelects = layui.formSelects;

        var tableins = table.render({
                elem: '#LAY-table'
                ,toolbar: '#toolbarDemo'
                ,title: "商品列表"
                ,id: 'goodslist'
                ,defaultToolbar: []
                ,url: "{{url('admin/goods/')}}/{{$index}}/table"
                ,method: "post"
                ,page: true
                ,limit:10
                ,cols: [[
                    {checkbox: true}
                    ,{field: 'photo', title: '缩略图',templet:function(d){return '<img src="'+ d.photo + '" />'}}
                    ,{field: 'sort', title: '排序',edit:true }
                    ,{field: 'title', title: '商品简称' }
                    ,{field: '', width:280 ,title: '商品信息',templet: function(d){
                        return  d.goods_name +'<br>'+ d.sku +'<br>' + d.goods_brand;
                    }}
                    ,{field: 'weight', title: '商品质量'}
                    ,{field: 'price', title: '销售价格' }
                    ,{field: 'is_sale', title: '会员价属性',templet: function(d){return d.is_sale == 1 ? "会员价" : "原价"}}
                    ,{field: 'is_coupon', title: '优惠券属性',templet: function(d){return d.is_coupon == 1 ? "可用" : "不可用" }}
                    ,{field: 'is_pay_balance', title: '支付属性',templet: function(d){return d.is_pay_balance == 1  ? "余额支付" : "不支持余额支付"}}
                    ,{field: 'stock', title: '库存'}
                    ,{field: 'amount_sum', title: '销量'}
                    ,{field: 'module', title: '所属模块'}
                    ,{field: 'effect', title: '功效'}
                    @if($index != 4)
                    ,{title: '操作', width:80 ,align: 'right', toolbar: '#table-toolbar'}
                    @endif
                ]]
            });

            table.on('edit(LAY-table)', function(obj){//更新sort 字段
                if(!/^[0-9]+$/.test(obj.value)){
                    layer.msg("格式不正确!");
                    return false;
                }
                $.ajax({
                    url: '/admin/goods/upsort'
                    , type: 'POST'
                    , dataType: 'json'
                    , data: {'id':obj.data.id,"value":obj.value}
                    , success: function (ret) {
                        if (ret.hasOwnProperty('code') && ret.code == 200) {
                            layer.msg(ret.message,{icon: 1,time:2000});
                            layer.close(index);
                            return false;
                        }
                        layer.msg(ret.message,{icon:2});
                    }
                });

            });
            table.on('tool(LAY-table)', function(obj){
                var id = obj.data.id;
                if(obj.event === 'del'){
                    layer.confirm('确认删除吗?', function(index){
                        $.ajax({
                            url: '/admin/goods/delete'
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'id':id}
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    obj.del();
                                    layer.close(index);
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });

                    });
                }
            });
            //监听工具条
            table.on('toolbar(LAY-table)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('确认删除吗?', function(index){
                        $.ajax({
                            url: '/admin/goods/'+ data.id
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'_method':"DELETE"}
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    obj.del();
                                    layer.close(index);
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });

                    });
                }else if(obj.event === 'export'){//export
                    var field = obj.config.where;

                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    var id_str = '';
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            id_str += data[i].id + ',';
                        }
                        id_str = id_str.substr(0, id_str.length-1);
                    }
                    layer.prompt({
                        id_str:id_str,
                        formType: 0,
                        value: '',
                        title: '请输入文件名称',
                        btn:['导出','取消']
                    }, function(value, index, elem){
                        if(value == '' || value == undefined){
                            layer.msg('请输入文件名称');
                        }else{
                            field.filename = value;
                            field.ids = id_str;
                            field.id = '{{$index}}';
                            console.log(field);
                            $.ajax({
                                url: '/admin/goods/export'
                                , type: 'POST'
                                , dataType: 'json'
                                , data: field
                                , success: function (ret) {
                                    if (ret.hasOwnProperty('code') && ret.code == 200) {
                                        layer.msg(ret.message,{icon: 1,time:2000});
                                        return false;
                                    }
                                    layer.msg(ret.message,{icon:2});
                                }
                            });
                            layer.close(index);
                        }

                    });
                }else if(obj.event === 'status') {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    var id_str = '';
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            id_str += data[i].id + ',';
                        }
                        id_str = id_str.substr(0, id_str.length-1);
                    }else{
                        layer.msg("请选择下架商品",{time:1000});
                        return false;
                    }
                    $.ajax({
                        url: '/admin/goods/{{$index}}/setStatus'
                        , dataType: 'json'
                        , data: {
                            'data':id_str
                            ,'status': {{$index}}
                        }
                        , success: function (ret) {
                            if (ret.hasOwnProperty('code') && ret.code == 200) {
                                layer.msg(ret.message,{icon: 1,time:2000});
                                table.reload('goodslist',{page:{curr:1}});
                                return false;
                            }else if(ret.hasOwnProperty('code') && ret.code == 201) {
                                layer.msg(ret.message,{icon: 2,time:2000});
                                table.reload('goodslist',{page:{curr:1}});
                                return false;
                            }
                            layer.msg(ret.message,{icon:2});

                        }
                    });
                }
            });
            //监听搜索
            form.on('submit(LAY-search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('goodslist', {
                    where: field,page: {curr: 1}
                });

            });

            $("#LAY-clear").click(function(){
                window.location.href='/admin/goods/'+ {{$index}} + '/list';
            });

        });
    </script>

@endsection